using Admin.Util;
using Furion;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;

namespace Admin.WebApi
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        private static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseIdHelper()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.Inject()
                        .UseStartup<Startup>();
                })
                .UseSerilogDefault(config =>
                {
                    if(App.Configuration["Serilog:WriteToConsole"] == "true")
                    {
                        config.WriteTo
                        .Console(
                            outputTemplate:
                            "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}");
                    }
                    if (App.Configuration["Serilog:WriteToFile"] == "true")
                    {
                        config.WriteTo.File("SysLogs/log.txt", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true);
                    }
                    if (App.Configuration["Serilog:WriteToMySQL"] == "true")
                    {
                        config.WriteTo.MySQL(App.Configuration["Serilog:LogMySQLDbContext"],
                            "sys_log",
                            LogEventLevel.Information);
                    }
                });
    }
}